package com.test.daily.leetcode.y2022.m06.day0606.v01;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @descriptions: Solution
 * @author: Tom
 * @date: 2022/6/6 下午 04:22
 * @version: 1.0
 */
public class Soultion {
    public static void main(String[] args) {

        MyCalender myCalender = new Soultion.MyCalender();
        // [], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]
        System.out.println(myCalender.book(10, 20));
        System.out.println(myCalender.book(50, 60));
        System.out.println(myCalender.book(10, 40));
        System.out.println(myCalender.book(5, 15));
        System.out.println(myCalender.book(5, 10));
        System.out.println(myCalender.book(25, 55));
    }

    public static class TimeLine {
        int start;
        int end;

        public TimeLine(int start, int end) {
            this.start = start;
            this.end = end;
        }
    }

    public static class MyStartComparator implements Comparator<TimeLine> {
        @Override
        public int compare(TimeLine o1, TimeLine o2) {
            return o1.start - o2.start;
        }
    }
    public static class MyEndComparator implements Comparator<TimeLine> {
        @Override
        public int compare(TimeLine o1, TimeLine o2) {
            return o1.end - o2.end;
        }
    }

    public static class MyCalender {
        public MyCalender() {
            lines = new ArrayList<>();
        }
        public ArrayList<TimeLine> lines;
        public int book(int start, int end) {
            lines.add(new TimeLine(start, end));
            lines.sort(new MyStartComparator());
            PriorityQueue<TimeLine> pq = new PriorityQueue<>(new MyEndComparator());
            pq.add(lines.get(0));
            int max = 1;
            for (int i = 1; i < lines.size(); i++) {
                while(pq.size()!=0 && pq.peek().end <= lines.get(i).start){
                    pq.poll();
                }
                pq.add(lines.get(i));
                max = Math.max(max, pq.size());
            }
            return max;
        }
    }

}
